home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / gnu_oleo_1_2_2.lha / oleo-1.2.2 / sc.c < prev    next >
C/C++ Source or Header  |  1993-03-03  |  5KB  |  247 lines

  1. /*    Copyright (C) 1990, 1992, 1993 Free Software Foundation, Inc.
  2.  
  3. This file is part of Oleo, the GNU Spreadsheet.
  4.  
  5. Oleo is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. Oleo is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with Oleo; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "funcdef.h"
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include "sysdef.h"
  23. #include "global.h"
  24. #include "cell.h"
  25. #include "io-generic.h"
  26. #include "io-abstract.h"
  27. #include "io-utils.h"
  28. #include "lists.h"
  29. #include "ref.h"
  30. #include "parse.h"
  31. #include "regions.h"
  32.  
  33.  
  34.  
  35. /* This reads/writes a subset of the SC public-domain spreadsheet's
  36.    file-format.  Note that since SC has no way of encoding some information
  37.    about a cell, writing a spread out in SC format, then reading it back in
  38.    will result in the loss of some information (most important:  cell formats)
  39.  */
  40.  
  41. static int
  42. get_range (pp,rp)
  43.      char ** pp;
  44.       struct rng * rp;
  45. {
  46.     int byte;
  47.     char *p;
  48.     struct var *v;
  49.  
  50.  
  51.     while(isspace(**pp))
  52.         (*pp)++;
  53.     byte=parse_cell_or_range(pp,rp);
  54.     if(byte)
  55.         return 0;
  56.     for(p= *pp;*p && !isspace(*p);p++)
  57.         ;
  58.     v=find_var(*pp,p-*pp);
  59.     if(!v)
  60.         return 1;
  61.     *pp=p;
  62.     *rp=v->v_rng;
  63.     return 0;
  64. }
  65.  
  66. void
  67. sc_read_file (fp,ismerge)
  68.      FILE * fp;
  69.       int ismerge;
  70. {
  71.     char buf[2048];
  72.     int lineno;
  73.     char *ptr;
  74.     int n;
  75.     struct rng rng;
  76.     int olda0;
  77.  
  78.     olda0=a0;
  79.     a0=1;
  80.     lineno=0;
  81.     if(!ismerge)
  82.         clear_spreadsheet();
  83.     while(fgets(buf,sizeof(buf),fp)) {
  84.         lineno++;
  85.         if(lineno%50==0)
  86.             io_info_msg("Line %d",lineno);
  87.         if(buf[0]=='#' || buf[0]=='\n')
  88.             continue;
  89.         if(!strncmp(buf,"set ",4)) {
  90.             /* ... */
  91.         } else if(!strncmp(buf,"format ",7)) {
  92.             ptr=buf+7;
  93.             if(get_range(&ptr,&rng))
  94.                 continue;
  95.             n=astol(&ptr);
  96.             set_width(rng.lc,n);
  97.         } else if(!strncmp(buf,"hide ",5)) {
  98.             ptr=buf+5;
  99.             if(get_range(&ptr,&rng))
  100.                 continue;
  101.             set_width(rng.lc,0);
  102.         } else if(!strncmp(buf,"mdir ",5)) {
  103.             /* ... */
  104.         } else if(!strncmp(buf,"define ",7)) {
  105.             char *eptr;
  106.  
  107.             ptr=buf+7;
  108.             while(isspace(*ptr))
  109.                 ptr++;
  110.             if(*ptr!='"') {
  111.                 io_error_msg("Line %d: No starting \" in define",lineno);
  112.                 continue;
  113.             }
  114.             ptr++;
  115.             for(eptr=ptr; *eptr && *eptr!='"';eptr++)
  116.                 ;
  117.             if(!*eptr) {
  118.                 io_error_msg("Line %d: No starting \" in define",lineno);
  119.                 continue;
  120.             }
  121.             ptr=new_var_value(ptr,eptr-ptr,eptr+1);
  122.             if(ptr)
  123.                 io_error_msg("Line %d: %s",ptr);
  124.         } else if(!strncmp(buf,"leftstring ",11) ||
  125.               !strncmp(buf,"rightstring ",12)) {
  126.             CELL *cp;
  127.  
  128.             ptr=buf+11;
  129.             if(get_range(&ptr,&rng))
  130.                 continue;
  131.             while(isspace(*ptr))
  132.                 ptr++;
  133.             if(*ptr=='=')
  134.                 ptr++;
  135.             new_value(rng.lr,rng.lc,ptr);
  136.             cp=find_cell(rng.lr,rng.lc);
  137.             if(buf[0]=='l')
  138.                 SET_JST(cp,JST_LFT);
  139.             else
  140.                 SET_JST(cp,JST_RGT);
  141.  
  142.         } else if(!strncmp(buf,"let ",4)) {
  143.             ptr=buf+4;
  144.             if(get_range(&ptr,&rng))
  145.                 continue;
  146.             while(isspace(*ptr))
  147.                 ptr++;
  148.             if(*ptr=='=')
  149.                 ptr++;
  150.             new_value(rng.lr,rng.lc,ptr);
  151.         } else
  152.             io_error_msg("Line %d: Can't parse %s",lineno,buf);
  153.     }
  154.     a0=olda0;
  155.     io_recenter_all_win();
  156. }
  157.  
  158. static FILE *sc_fp;
  159. static struct rng *sc_rng;
  160. static void
  161. sc_write_var (name,var)
  162.      char * name;
  163.       struct var * var;
  164. {
  165.     if(var->var_flags==VAR_UNDEF && (!var->var_ref_fm || var->var_ref_fm->refs_used==0))
  166.         return;
  167.     switch(var->var_flags) {
  168.     case VAR_UNDEF:
  169.         break;
  170.     case VAR_CELL:
  171.         if(var->v_rng.lr>=sc_rng->lr && var->v_rng.lr<=sc_rng->hr && var->v_rng.lc>=sc_rng->lc && var->v_rng.lc<=sc_rng->hc)
  172.             (void)fprintf(sc_fp,"define \"%s\" %s\n",var->var_name,cell_name(var->v_rng.lr,var->v_rng.lc));
  173.         break;
  174.     case VAR_RANGE:
  175.         if(var->v_rng.lr<sc_rng->lr || var->v_rng.hr>sc_rng->hr || var->v_rng.lc<sc_rng->lc || var->v_rng.hc>sc_rng->hc)
  176.             break;
  177.  
  178.         (void)fprintf(sc_fp,"define \"%s\" %s\n",var->var_name,range_name(&(var->v_rng)));
  179.         break;
  180. #ifdef TEST
  181.     default:
  182.         panic("Unknown var type %d",var->var_flags);
  183.         break;
  184. #endif
  185.     }
  186. }
  187.     
  188. void
  189. sc_write_file (fp,rng)
  190.      FILE * fp;
  191.       struct rng * rng;
  192. {
  193.     unsigned short w;
  194.     CELLREF r,c;
  195.     CELL *cp;
  196.     char *ptr;
  197.     int olda0;
  198.  
  199.     if(!rng)
  200.         rng= &all_rng;
  201.  
  202.     olda0=a0;
  203.     a0=1;
  204.     (void)fprintf(fp,"# This file was created by Oleo, for use by the Spreadsheet Calculator\n");
  205.     (void)fprintf(fp,"# You probably don't want to edit it.\n\n");
  206.  
  207.     find_widths(rng->lc,rng->hc);
  208.     while(w=next_width(&c))
  209.         fprintf(fp,"format %s %d ???\n",cell_name(MIN_ROW,c),w);
  210.     sc_fp=fp;
  211.     sc_rng=rng;
  212.     for_all_vars(sc_write_var);
  213.     find_cells_in_range(rng);
  214.     while(cp=next_row_col_in_range(&r,&c)) {
  215.         switch(GET_TYP(cp)) {
  216.         case TYP_STR:
  217.             if((GET_JST(cp)==JST_DEF && default_jst==JST_RGT) || GET_JST(cp)==JST_RGT)
  218.                 ptr="right";
  219.             else ptr="left";
  220.             fprintf(fp,"%sstring %s = %s\n",ptr,cell_name(r,c),decomp(r,c,cp));
  221.             decomp_free();
  222.             break;
  223.         case 0:
  224.             break;
  225.         default:
  226.             fprintf(fp,"let %s = %s\n",cell_name(r,c),decomp(r,c,cp));
  227.             decomp_free();
  228.             break;
  229.         }
  230.     }
  231.     a0=olda0;
  232. }
  233.  
  234. int
  235. sc_set_options (set_opt,option)
  236.      int set_opt;
  237.       char * option;
  238. {
  239.     return -1;
  240. }
  241.  
  242. void
  243. sc_show_options ()
  244. {
  245.     io_text_line("File format: sc  (Public domain spreadsheet calculator)");
  246. }
  247.